Intro
Intersection observer
Component to check if it is inside the viewport.
The most basic setup is adding the Intersection observer component anywhere on your page. The defaults mean that the "Change" event will be called each time the component is entirely inside the viewport and when it leaves. You can check the event "isIntersection" to check if it entered or left the view.
By setting the "threshold"-attribute, to 0 means it will intersect when any part of the element enters the viewport. 0.5 means half of it is inside.
If you want to continuously know how much of the element is inside the view, you can add an array to the threshold instead. The change event will be called each time any of the thresholds enter/leave. You can check the event "intersectionRatio" to see which threshold has been reached.
Demo
Scroll container and notice how the card s fading
in based on how far into the viewport it gets.
Detect Element Visibility
The Intersection Observer API allows you to efficiently detect when an element enters or leaves the viewport, enabling smooth lazy loading and infinite scrolling experiences.
Trigger Animations
Use the Intersection Observer API to trigger animations as elements come into view, creating engaging and interactive user experiences without affecting performance.
Optimize Resource Loading
Improve page load times and reduce bandwidth usage by leveraging the Intersection Observer API to load images, videos, and entire components only when they're about to enter the viewport.
Components
Intersection observer
The sole component of this package. Can be used to play animations when elements come into view, or to delay rendering entirely before a container is in the viewport.
Attributes
root-element-id The id of the element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null. root-margin Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros. threshold Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback to run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible. Events
Change Called each time a threshold is met. Event object matches IntersectionObserverEntry Context
In view Any subcomponent can subscribe to this value. Will indicate if any threshold is met. Intersection ratio Any subcomponent can subscribe to this value. Indicates how much of the component is intersected. Useful for animations based on scroll.
Formulas
Build Threshold List
Helper formula to construct high definition threshold lists. By default thresholds is a single value to detect whether the component is intersecting or not, but it is possible to add multiple values to get a continuous intersection ratio. This is particularly useful for animations that are bound to page or container scrolling.
Number of steps How precisely you want to report steps. Often a lower step count together with CSS transitions can lead to performant and smooth transitions.